Imports Iesi.Collections.Generic
Imports KanBan.Pgd.Domain.Exceptions
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Common
Imports NUnit.Framework
Imports System.Transactions
Imports KanBan.Pgd.Infrastructure.Repositories
Imports KanBan.Pgd.Domain.ValueObjects

<TestFixture()> _
Public Class ProvisioningTest

    Private scope As TransactionScope = Nothing

    <SetUp()> _
    Public Sub Init()
        scope = New TransactionScope()
    End Sub

    <TearDown()> _
    Public Sub Dispose()
        If Not scope Is Nothing Then
            scope.Dispose()
            scope = Nothing
        End If
    End Sub

    <Test()> _
    Public Sub GetAllGroupsTest()

        Dim groupRepository As New GroupRepository()

        Dim groups As New List(Of Group)(groupRepository.GetAll())

        Assert.IsTrue(groups.Count > 0)

        For Each g As Group In groups
            Assert.IsNotNull(g.Id)
        Next

    End Sub

    <Test()> _
    Public Sub GetAllUsersInGroup()

        Dim groupRepository As New GroupRepository()

        Dim users As ISet(Of User) = groupRepository.GetById(TestConstants.AdministratorsGroupId).Users

        Assert.IsTrue(users.Count > 0)

    End Sub

    <Test()> _
    Public Sub GetUserPermissionsTest()

        Dim userRepository As New UserRepository()

        Dim user As User = userRepository.GetById(TestConstants.AdministratorUserId)

        Dim permissions As IList(Of Permission) = userRepository.GetAllPermissions(user.Id) 'user.GetAllPermissions()

        Assert.AreEqual(3, permissions.Count)

    End Sub

    <Test()> _
    Public Sub AddUserPermissionTest()

        Dim userRepository As New UserRepository()
        Dim user As User = userRepository.GetById(TestConstants.AdministratorUserId)
        Dim permission As Permission = ValueObjectRepository(Of Permission).GetById(TestConstants.TestPermissionId)

        user.AddPermission(permission)

        Dim userPermissions As ISet(Of Permission) = userRepository.GetById(TestConstants.AdministratorUserId).IndividualPermissions

        Dim hasTestPermission As Boolean

        For Each p As Permission In userPermissions
            If p.Id = TestConstants.TestPermissionId Then
                hasTestPermission = True
                Exit For
            End If
        Next

    End Sub

    '<Test()> _
    'Public Sub RemoveUserPermissionTest()

    '    Dim userRepository As New UserRepository()
    '    Dim user As User = userRepository.GetById(TestConstants.AdministratorUserId)

    '    user.RemovePermission(TestConstants.RemoveUsersPermissionId)

    '    Dim savedUser As User = userRepository.SaveOrUpdate(user)

    '    Assert.IsFalse(savedUser.HasPermission(TestConstants.RemoveUsersPermissionId))

    'End Sub

    <Test()> _
    Public Sub SaveNewGroupTest()

        Dim groupRepository As New GroupRepository()

        Dim group As New Group(TestConstants.TestGroupName)

        Dim savedGroup As Group = groupRepository.SaveOrUpdate(group)

        Assert.IsNotNull(savedGroup)
        Assert.AreEqual(group.Name, savedGroup.Name)

    End Sub

    <Test()> _
    <ExpectedException(GetType(BusinessRuleException))> _
    Public Sub SaveGroupWithSameNameTwice()

        Dim groupRepository As New GroupRepository()

        Dim group1 As New Group(TestConstants.TestGroupName)
        Dim group2 As New Group(TestConstants.TestGroupName)

        groupRepository.SaveOrUpdate(group1)
        groupRepository.SaveOrUpdate(group2)

    End Sub

    <Test()> _
    Public Sub EditGroupTest()

        Dim groupRepository As New GroupRepository()

        Dim group As Group = groupRepository.GetById(TestConstants.AdministratorsGroupId)

        group.Name = "Modified Group Name"

        groupRepository.SaveOrUpdate(group)

        Dim updatedGroup As Group = groupRepository.GetById(group.Id)

        Assert.IsNotNull(updatedGroup)
        Assert.AreEqual(group.Name, updatedGroup.Name)

    End Sub

    <Test()> _
    Public Sub AddUserToGroupTest()

        Dim groupRepository As New GroupRepository()
        Dim userRepository As New UserRepository()

        Dim user As User = userRepository.GetById(TestConstants.EmployeeUserId)
        Dim group As Group = groupRepository.GetById(TestConstants.AdministratorsGroupId)

        group.AddUser(user)

        Dim savedGroup As Group = groupRepository.SaveOrUpdate(group)

        Dim userIsInGroup As Boolean = False

        For Each u As User In savedGroup.Users
            If u.Id = user.Id Then
                userIsInGroup = True
                Exit For
            End If
        Next

        Assert.IsTrue(userIsInGroup)

    End Sub

    <Test()> _
    Public Sub RemoveUserFromGroupTest()

        Dim groupRepository As New GroupRepository()
        Dim userRepository As New UserRepository()

        Dim user As User = userRepository.GetById(TestConstants.EmployeeUserId)
        Dim group As Group = groupRepository.GetById(TestConstants.EmployeesGroupId)

        group.RemoveUser(user)

        groupRepository.SaveOrUpdate(group)

        Dim savedGroup As Group = groupRepository.GetById(group.Id)

        Dim userIsInGroup As Boolean = False

        For Each u As User In savedGroup.Users
            If u.Id = TestConstants.AdministratorUserId Then
                userIsInGroup = True
                Exit For
            End If
        Next

        Assert.IsFalse(userIsInGroup)

    End Sub

    <Test()> _
    Public Sub AddUserTest()

        Dim userRepository As New UserRepository()

        Dim profileRepository As New ProfileRepository()

        Dim testProfile As Profile = profileRepository.GetById(TestConstants.TestProfileId)

        Dim newUser As New User("Latino Solanas", "LSolanas", "test#1234", testProfile)

        Dim addedUser As User = userRepository.SaveOrUpdate(newUser)

        Assert.AreEqual(newUser.Name, addedUser.Name)
        Assert.AreEqual(newUser.Name, addedUser.Name)

    End Sub

    <Test()> _
    Public Sub DeleteUserTest()

        Dim userRepository As New UserRepository()

        userRepository.DeleteLogical(TestConstants.EmployeeUserId)

        Dim user As User = userRepository.GetById(TestConstants.EmployeeUserId)

        Assert.IsTrue(user.IsDeleted)

    End Sub

    <Test()> _
    Public Sub EditUserTest()

        Dim userRepository As New UserRepository()

        Dim user As User = userRepository.GetById(TestConstants.AdministratorUserId)

        user.Name = "Modified Administrator Name"

        Dim updatedUser As User = userRepository.GetById(user.Id)

        Assert.AreEqual(user.Name, updatedUser.Name)

    End Sub

    <Test()> _
    Public Sub GetUserTest()

        Dim userRepository As New UserRepository()

        Dim user As User = userRepository.GetById(TestConstants.AdministratorUserId)

        Assert.IsNotNull(user)
        Assert.AreEqual("Administrator", user.UserName)

    End Sub



End Class
